package com.helloae;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.geodatabase.FeatureClassName;
import com.esri.arcgis.geodatabase.FeatureDataConverter;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.PropertySet;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 将shp插入到SDE Geodatabase中
 * @author zhushijie
 */
public class SHPToSDE {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		//这里的License需要注意，esriLicenseProductCodeArcView和esriLicenseProductCodeEngine不能满足需求
		//必须使用其他的License，比如esriLicenseProductCodeArcEditor、esriLicenseProductCodeEngineGeoDB、esriLicenseProductCodeArcServer、esriLicenseProductCodeArcInfo
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
		
		//创建用于输入的IFeatureClassName并配置其所需的参数
		WorkspaceName pInShpWorkspaceName = new WorkspaceName();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		pInShpWorkspaceName.setPathName(ecliseWorkspace+"HelloAE"+File.separator+"DataSet"+File.separator+"chap06");
		pInShpWorkspaceName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
		FeatureClassName pShpDatasetName = new FeatureClassName();
		pShpDatasetName.setWorkspaceNameByRef(pInShpWorkspaceName);
		pShpDatasetName.setName("soils");
		
		WorkspaceName pSDEWorkspaceName = new WorkspaceName();
		pSDEWorkspaceName.setWorkspaceFactoryProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
		FeatureClassName pSDEDatasetName = new FeatureClassName();
		
		PropertySet prop = new PropertySet();
		prop.setProperty("SERVER", "192.168.3.106");
		prop.setProperty("INSTANCE", "5151");
		prop.setProperty("DATABASE", "sde");
		prop.setProperty("USER", "sde");
		prop.setProperty("PASSWORD", "sde");
		prop.setProperty("VERSION", "SDE.DEFAULT");
		
		//这样可以获得SDE GeoDatabase的Workspace，不过这里不需要
//		SdeWorkspaceFactory pwf = new SdeWorkspaceFactory();
//		IWorkspace pw = pwf.open(prop, 0);
		
		pSDEWorkspaceName.setConnectionProperties(prop);
		pSDEDatasetName.setWorkspaceNameByRef(pSDEWorkspaceName);
		pSDEDatasetName.setName("soils");
		
		FeatureDataConverter pShpToFC = new FeatureDataConverter();
		pShpToFC.convertFeatureClass(pShpDatasetName, null, null, pSDEDatasetName, null, null, "", 1000, 0);
		
		ao.shutdown();
		System.out.println("Congratulaction!");
	}

}
